package com.dc.commons.resolver;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.dc.commons.enumer.MoveType;
import com.dc.commons.query.DcQuery;
import com.dc.commons.servlet.Servlets;
import com.google.common.collect.Maps;

/*******************************************************************************<P>
 * 文件名: DcQueryHandlerMethodArgumentResolver.java <br>
 * 版本: 1.0<br>
 * 描述:<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年5月31日 上午9:31:47<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
public class DcQueryHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

	
	public PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver;
	public DcQueryHandlerMethodArgumentResolver(PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver) {
		this.pageableHandlerMethodArgumentResolver=pageableHandlerMethodArgumentResolver;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.web.method.support.HandlerMethodArgumentResolver#supportsParameter(org.springframework.core.MethodParameter)
	 */
	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return DcQuery.class.equals(parameter.getParameterType());
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.method.support.HandlerMethodArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.method.support.ModelAndViewContainer, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.bind.support.WebDataBinderFactory)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		DcQuery dcQuery=new DcQuery();
		dcQuery.setRequest(webRequest);
		Pageable pageable=pageableHandlerMethodArgumentResolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
		dcQuery.setPageable(pageable);
		Map<String,Object> map=Maps.newHashMap();
		dcQuery.setQueryMap(Servlets.getParametersStartingWith(webRequest, "search_", map));
		
		Map<String,Object> export=Maps.newHashMap();
		Map excelMap=Servlets.getParametersStartingWith(webRequest, "excel_", export);
		dcQuery.setExceltMap(excelMap);
		
		String[] ids=webRequest.getParameterValues("ids");
		if(ids!=null&&ids.length==1){
			dcQuery.setIds(ids[0].split(","));
		}else{
			dcQuery.setIds(ids);
		}
		String realPath=webRequest.getNativeRequest(HttpServletRequest.class).getSession().getServletContext().getRealPath("/");
		dcQuery.setRealPath(realPath);
		
		dcQuery.setFrom(webRequest.getParameter("from"));
		dcQuery.setTo(webRequest.getParameter("to"));
		String moveType=webRequest.getParameter("moveType");
		if(moveType!=null){
			dcQuery.setMoveType(MoveType.valueOf(moveType));
		}
		
		
		/**
		 * 是否下载模板 如果有 downloadTemplate 参数 且为true 则设置为 end 可以直接下载模板
		 */
		String downloadTemplate=webRequest.getParameter("downloadTemplate");
		if(downloadTemplate!=null&&downloadTemplate.equals("true")){
			dcQuery.setEnd(true);
		}
		return dcQuery;
	}

}
